<?php
/**
 *	后台管理系统-首页
 */
namespace app\bews\controller;
use app\bews\controller\Base;
use think\facade\Db;
use think\facade\View;
use think\facade\Env;
use think\facade\Config;

class Index extends Base{
	# 首页
	public function index(){
		$menus = [];
		$where = ['group_id'=>$this->aUser['group_id']];
		$role = Db::table('bew_admin_user_group')->where($where)->find();
		if($role){
			$role['rights'] = (isset($role['rights']) && $role['rights']) ? json_decode($role['rights'],true) : [];
		}
		if($role['rights']){
			$where = [
				['smid','in',implode(',',$role['rights']) ],
				['status','=',1],
				['is_show','=',1]
			];
			$menus = Db::table('bew_admin_sys_menu')->order('type,sort desc')->where($where)->select();
			foreach($menus as $menus_v){
				if($menus_v['parent_id'] == 0){
					$menu[$menus_v['smid']] = $menus_v;
				}else{
					$menu[$menus_v['parent_id']]['children'][] = $menus_v;
				}
			}
		}
		View::assign([
			'role' => $role,
			'menu' => $menu
		]);
		return View::fetch();
	}
	# 欢迎页面
	public function welcome(){
		View::assign([
			'time' => date('Y-m-d',$_SERVER['REQUEST_TIME']),
		]);
		return View::fetch();
	}
	# 图片上传
	public function upload_img(){
		$file = request()->file();
		$files = request()->file('file');
		if($file==null){
			exit(json_encode(array('code'=>1,'msg'=>'没有文件上传')));
		}
		try {
			validate(['image'=>'filesize:10240|fileExt:jpg,png,gif,jpeg'])->check($file);
			$info = \think\facade\Filesystem::disk('public')->putFile('bews',$files);
		} catch (\think\exception\ValidateException $e) {
			exit(json_encode(array('code'=>1,'msg'=>$e->getMessage())));
		}
		$info = str_replace("\\","/",$info);
		$img = '/storage/'.$info;
		exit(json_encode(array('code'=>0,'data'=>$img,'url'=>$this->config['admin_domain'].$img)));
	}
	# 富文本图片上传
	public function upload_imgs(){
		$file = request()->file();
		$files = request()->file('file');
		if($file==null){
			exit(json_encode(array('code'=>1,'msg'=>'没有文件上传')));
		}
		try {
			validate(['image'=>'filesize:10240|fileExt:jpg,png,gif,jpeg'])->check($file);
			$info = \think\facade\Filesystem::disk('public')->putFile('bews',$files);
		} catch (\think\exception\ValidateException $e) {
			exit(json_encode(array('code'=>1,'msg'=>$e->getMessage())));
		}
		$info = str_replace("\\","/",$info);
		$img = '/storage/'.$info;
		exit(json_encode(array('code'=>0,'data'=>['src'=>$img])));
	}
	# 富文本图片上传
	public function upload_imgs_kin(){
		$file = request()->file();
		$files = request()->file('imgFile');

		if($file==null){
			exit(json_encode(array('error'=>1,'message'=>'没有文件上传')));
		}
		try {
			validate(['image'=>'filesize:10240|fileExt:jpg,png,gif,jpeg'])->check($file);
			$info = \think\facade\Filesystem::disk('public')->putFile('bews',$files);
		} catch (\think\exception\ValidateException $e) {
			exit(json_encode(array('error'=>1,'message'=>$e->getMessage())));
		}
		$info = str_replace("\\","/",$info);
		$img = '/storage/'.$info;
		exit(json_encode(array('error'=>0,'url'=>$img)));
	}
	# 清除缓存
	public function clear(){
		$a = delete_dir_file(Env::get('runtime_path').'cache/');
		$b = delete_dir_file(Env::get('runtime_path').'temp/');
		if ($a || $b) {
			$this->returnCode(0);
		} else {
			$this->returnCode('91000006');
		}
	}
}